package com.lab303.powerproject.service.impl;

import com.lab303.powerproject.entity.ItemInfo;
import com.lab303.powerproject.entity.Manager;
import com.lab303.powerproject.repository.ItemInfoRepository;
import com.lab303.powerproject.repository.ManagerRepository;
import com.lab303.powerproject.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerRepository managerRepository;

    @Autowired
    private ItemInfoRepository itemInfoRepository;

    @Override
    public Manager loginIssucceed(String managerName, String managerPwd){
        return managerRepository.findManagerByManagerNameAndManagerPwd(managerName,managerPwd);
    }

    @Override
    public Manager registManager(Manager manager){return managerRepository.saveAndFlush(manager);}

    @Override
    public Manager registNameIsExist(String managerName){
        return managerRepository.findManagerByManagerName(managerName);
    }

    @Override
    public ItemInfo registRemarkIsExist(String remark){
        return itemInfoRepository.findItemInfoByRemark(remark);
    }

    @Override
    public boolean deleteManagerById(Integer managerId){
        Manager manager = managerRepository.findById(managerId).orElse(null);
        if(manager != null){
            managerRepository.deleteById(managerId);
            return true;
        }
        return false;
    }

    @Override
    public void updateManager(Manager manager){
        managerRepository.updateManager(manager.getManagerName(),manager.getManagerPwd(),manager.getRemark(),manager.getManagerId());
    }

    @Override
    public List<Manager> getAllManager(){return managerRepository.findAll();}
}
